package com.example.codegenerator.util;

public class StringUtils {
    public static String getBasePath(String basePath) {
        if(!basePath.contains("/src/main/java/") && !basePath.contains("\\src\\main\\java\\")) {
            System.err.println("路径不包含/src/main/java/，请检查配置的目录");
            return null;
        }
        if(basePath.contains("\\")) {
            basePath = basePath.replace("\\", "/");
            if(basePath.endsWith("/")) {
                basePath = basePath.substring(0, basePath.length() - 1);
            }
        }
        return basePath;
    }

    public static String getBasePackage(String basePath) {
        basePath = getBasePath(basePath);
        assert basePath != null;
        String basePackageAll = basePath.replace("/", ".");
        return basePackageAll.substring(basePackageAll.indexOf(".src.main.java.") + ".src.main.java.".length());
    }

    public static String getMethodName(String fieldName, String preffix) {
        return preffix + String.valueOf(fieldName.charAt(0)).toUpperCase() + fieldName.substring(1);
    }

    public static String getFieldName(String table_element) {
        table_element = table_element.toLowerCase();
        byte[] buffers = table_element.getBytes();

        for(int i = 1; i < buffers.length - 1; ++i) {
            if (buffers[i] == 95) {
                buffers[i + 1] = (byte)(buffers[i + 1] - 32);
            }
        }

        return (new String(buffers)).replaceAll("_", "");
    }

    public static String getFieldTypeName(String columnType) {
        if (columnType.toUpperCase().startsWith("INT(")
            || columnType.equalsIgnoreCase("INT")) {
            return "Integer";
        } else if (columnType.toUpperCase().startsWith("VARCHAR(")
            || columnType.toUpperCase().startsWith("VARCHAR2(")
            || columnType.equalsIgnoreCase("VARCHAR")
            || columnType.equalsIgnoreCase("VARCHAR2")
        ) {
            return "String";
        } else if(columnType.toUpperCase().startsWith("DECIMAL(")
                || columnType.equalsIgnoreCase("DECIMAL")
                || columnType.toUpperCase().startsWith("NUMERIC(")
                || columnType.equalsIgnoreCase("NUMERIC")) {
            return "BigDecimal";
        } else {
            return columnType.equalsIgnoreCase("TIMESTAMP") ? "Timestamp" : "Object";
        }
    }

    public static String getJdbcTypeName(String columnType) {
        if (columnType.toUpperCase().startsWith("INT(")
                || columnType.equalsIgnoreCase("INT")) {
            return "INTEGER";
        } else if (columnType.toUpperCase().startsWith("VARCHAR(")
                || columnType.toUpperCase().startsWith("VARCHAR2(")
                || columnType.equalsIgnoreCase("VARCHAR")
                || columnType.equalsIgnoreCase("VARCHAR2")) {
            return "VARCHAR";
        } else if(columnType.toUpperCase().startsWith("DECIMAL(")
                || columnType.equalsIgnoreCase("DECIMAL")
                || columnType.toUpperCase().startsWith("NUMERIC(")
                || columnType.equalsIgnoreCase("NUMERIC")) {
            return "DECIMAL";
        } else if(columnType.toUpperCase().startsWith("TIMESTAMP")) {
            return "TIMESTAMP";
        } else {
            return "VARCHAR";
        }
    }
}
